﻿namespace Voxel2Unity
{
    using System;
    using System.IO;
    using System.Runtime.InteropServices;
    using System.Text;
    using UnityEngine;

    [StructLayout(LayoutKind.Sequential)]
    public struct Util
    {
        public static string Load(string _path)
        {
            try
            {
                StreamReader reader = File.OpenText(_path);
                string str = reader.ReadToEnd();
                reader.Close();
                return str;
            }
            catch (Exception)
            {
                return string.Empty;
            }
        }

        public static void Save(string _data, string _path)
        {
            try
            {
                FileStream stream = new FileStream(_path, FileMode.Create);
                StreamWriter writer = new StreamWriter(stream, Encoding.UTF8);
                writer.Write(_data);
                writer.Close();
                stream.Close();
            }
            catch (Exception)
            {
            }
        }

        public static byte[] FileToByte(string path)
        {
            if (!File.Exists(path))
            {
                return null;
            }
            byte[] buffer = null;
            try
            {
                buffer = File.ReadAllBytes(path);
            }
            catch
            {
                return null;
            }
            return buffer;
        }

        public static bool ByteToFile(byte[] bytes, string path)
        {
            try
            {
                CreateFolder(new FileInfo(path).Directory.FullName);
                FileStream stream = new FileStream(path, FileMode.Create, FileAccess.Write);
                stream.Write(bytes, 0, bytes.Length);
                stream.Close();
                stream.Dispose();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public static void CreateFolder(string _path)
        {
            _path = GetFullPath(_path);
            if (!Directory.Exists(_path))
            {
                string fullName = new FileInfo(_path).Directory.FullName;
                if (Directory.Exists(fullName))
                {
                    Directory.CreateDirectory(_path);
                }
                else
                {
                    CreateFolder(fullName);
                    Directory.CreateDirectory(_path);
                }
            }
        }

        public static string FixPath(string _path)
        {
            _path = _path.Replace('\\', '/');
            _path = _path.Replace("//", "/");
            while ((_path.Length > 0) && (_path[0] == '/'))
            {
                _path = _path.Remove(0, 1);
            }
            return _path;
        }

        public static string GetFullPath(string path)
        {
            return new FileInfo(path).FullName;
        }

        public static string RelativePath(string path)
        {
            path = FixPath(path);
            if (path.StartsWith("Assets"))
            {
                return path;
            }
            if (path.StartsWith(FixPath(Application.dataPath)))
            {
                return ("Assets" + path.Substring(Application.dataPath.Length));
            }
            return string.Empty;
        }

        public static string CombinePaths(params string[] paths)
        {
            string str = string.Empty;
            for (int i = 0; i < paths.Length; i++)
            {
                str = Path.Combine(str, FixPath(paths[i]));
            }
            return FixPath(str);
        }

        public static string GetExtension(string path)
        {
            return Path.GetExtension(path);
        }

        public static string GetName(string path)
        {
            return Path.GetFileNameWithoutExtension(path);
        }

        public static string ChangeExtension(string path, string newEx)
        {
            return Path.ChangeExtension(path, newEx);
        }

        public static bool PathIsDirectory(string path)
        {
            return ((File.GetAttributes(path) & FileAttributes.Directory) == FileAttributes.Directory);
        }
    }
}

